$(function () {
  if (getUrlParam('tab')) {
    $('#loading').hide();
    $('.j-page-box').show();
    return;
  }

  let data_list_baidu = [];
  let data_list_tsou = [];

  function mobileMergeSearch() {
    for (let i = 0; i < data_list_baidu.length; i++) {
      const item_baidu = data_list_baidu[i];
      for (let j = 0; j < data_list_tsou.length; j++) {
        const item_tsou = data_list_tsou[j];
        if (item_baidu == item_tsou) {
          $(`.search_list_baidu > .search_item:nth-child(${i + 1}) a`).first().prepend('👍🏻 ');
          $(`.search_list_tsou > .search_item:nth-child(${j + 1}) h3`).first().prepend('👍🏻 ');
        }
      }
    }
  }

  const kw = $('#kw_hidden').val();
  const page = $('#page_hidden').val();
  const search_data = {kw, page};

  $.ajaxSetup({
    beforeSend: function (xhr, settings) {
      xhr.setRequestHeader('x-csrf-token', $.cookie('csrfToken'));
    },
  });

  $.post('/searchBaidu', search_data, function (result) {
    $('#loading').hide();
    if (result.code == 403) {
      return;
    }

    data_list_baidu = result.data.map(v => v.key);
    result.data.forEach(v => {
      $('.search_list_baidu').append(`<div class="search_item">${v.content}</div`);
    });

    if (result.data.length > 0) {
      mobileMergeSearch();
      $('.search_list_tsou').show();
      $('.j-page-box').show();
    } else {
      $('.empty-result-container').show();
    }
  });

  $.post('/searchTsou', search_data, function (result) {
    if (result.code == 403) {
      return;
    }

    data_list_tsou = result.data.map(v => v.key);
    result.data.forEach(v => {
      try {
        $('.search_list_tsou').append(`<div class="search_item">${v.content}</div`);
      } catch (e) {
        console.warn(e.message);
      }
    });

    if (result.data.length > 0) {
      mobileMergeSearch();
    }
  });


  // if (page == 1) {
  //   $.post('/searchZhihu', search_data, function (result) {
  //     result.data.forEach(v => {
  //       $('.search_list_zhihu').append(`<div class="search_item"><img class="search_item_logo search_item_logo_zhihu" src="${v.logo}">${v.content}</div`);
  //     });
  //   });
  // }


  $('body').on('click', '.image-one-line-item_aBS4H,.image-two-line-item_1rPTg', function (evt) {
    $(evt.currentTarget).attr('href', $(evt.currentTarget).find('img').attr('src'));
  });
});


let A = {
  setup: () => {
  },
  init: () => {
  }
};

let google = {c: {}};


